(function(window) {

var icons = {

};

var iconClicked = function($dom, name) {

	console.warn('clicked icon ' + name);
	
	var iconID = $dom.data('id');
	if(iconID) {
		switch(iconID) {
			case 'contact-financial':
				pf.overlay('image', {imgID: 'contact-financial'});
				break;
			case 'contact-puxicenter':
				pf.overlay('image', {imgID: 'contact-puxicenter'});
				break;
			case 'contact-travel':
				pf.overlay('image', {imgID: 'contact-travel'});
				break;
			case 'contact-golf':
				pf.overlay('image', {imgID: 'contact-golf'});
				break;
			case 'hidepdf':
				pcc2.hidepdf();
				break;
			case 'contact-snowforce':
				pf.overlay('image', {imgID: 'contact-snowforce'});
				break;
			case 'contact-driving':
				pf.overlay('image', {imgID: 'contact-PDS'});
				break;
		}
		return;
	}

	switch(name) {
		case 's_a_pds_icon':
			pcc2.pdf();
			pf.emitPage($("#viewerContainer"));
			break;
		case 'main_s_a_icon':
			pf.goPage('show', {showID: 's_a'});
			break;
		case 'main_motorsport_icon':
			pf.goPage('show', {showID: 'motorsport'});
			break;
		case 'main_events_icon':
			pf.goPage('show', {showID: 'events'});
			break;
		case 'home_icon':
			pf.goPage('show', {showID: 'wdy'});
			break;
		// Hamburger
		case 'menu_burger_btn':
			pf.overlay('topic');
			break;
		// Close
		case 'close_b':
			if (pf.overlayPage=="play_video") {
				pcc2.video_close_detail();
			};
			if($dom.parent().hasClass('overlay'))
				pf.hideOverlay();
			break;
		// Test Drive
		case 'test_drive_r':
			pf.overlay('image', {imgID: 'testdrive',page_car:$("#"+pf.curPage).attr("page_show")});
			break;
		case 'test_drive_b':
			if (pcc2.$video.is(":hidden")) {
				if ($("#features").find("video").length>0) {
					$("#features").find("video").trigger('pause');
				};
			}else{
				pcc2.$video.find("video").trigger('pause');
			}
			pf.overlay('image', {imgID: 'testdrive',page_car:"wdy_qrcode"});
			$('body').addClass('wdy');
			$('.operate-zoon').removeClass("test_drive_b_show");
			break;
		// Features
		case 'features_icon':
			var small_car=null;
			var Lang_key=null;
			var car_tittle=null;
			var page    = pf.pages[pf.curPage];
			var classID = page.data && page.data.id;
			switch(classID){
			case "911":
			cars_name="911";
			Lang_key="911_carrera";
			car_tittle="911 Carrera";
			break;
			case "boxster":
			cars_name="boxster";
			Lang_key="boxster";
			car_tittle="Boxster";
			break;
			case "cayman":
			cars_name="cayman";
			Lang_key="cayman";
			car_tittle="Cayman";
			break;
			case "cayenne":
			cars_name="Cayenne";
			Lang_key="Cayenne";
			car_tittle="Cayenne";
			break;
			case "macan":
			cars_name="macan";
			Lang_key="Macan";
			car_tittle="Macan";
			break;
			case "panamera":
			cars_name="panamera";
			Lang_key="panamera";
			car_tittle="Panamera";
			break;
			default:
			// alert("这车失踪了");
			break;
		}

			// pf.overlay("overlay",{big:page.big_car,small_car:small_car});
			pf.overlay("features",{car_name:cars_name,lang_key:Lang_key,tittle:car_tittle,page_car:$("#"+pf.curPage).attr("page_show")});
			// alert(classID);
			break;
		// Contact
		case 'contact_btn':
			pf.overlay('image', {imgID: 'puxi'});
			break;
		// STOCK LIST
		case 'stock_btn':
			// alert('STOCK LIST');
			pf.goPage("vehicleList");
			break;
		// Cinema
		case 'cinema_btn':
			pf.goPage('movie');
			break;
		// Floorplan
		case 'floorplan_btn':
			pf.overlay('image', {imgID: 'floorplan'});
			// pf.goPage('image', {imgID: 'floorplan'});
			break;
		case 'return_b':
			pf.goBack();
			// var page = pf.pages[pf.curPage];
			// page['onBack'] && page['onBack']();
			break;
		default:
			// Models
			if(name.indexOf('car_')>-1 && name.indexOf('_icon')>-1) {
				var cardID = name.replace('car_', '').replace('_icon', '');
				// console.warn(cardID);
				// alert(cardID);
				pf.overlay("vehicle_small_List", {
					car_name: cardID,
					force:    true,
					notJump:  true
				});
			}
			break;
	}
};

icons.init = function() {
	$(document).on('click', '.icon-wrap', function() {
		var $dom = $(this);
		var name = $dom.data('name');
		pcc2.css3($dom.find('.liao_css3'));
		// window.setTimeout(function() {
			iconClicked($dom, name);
			// return false;
		// }, 500);
	});
};

icons.render = function($el, btns) {
	var tpl  = '<div data-id="%s" data-name="%s"  class="so-btn icon-wrap"><div class="liao_css3 hi-icon hi-icon-mobile %s"><div class="icon-btn  sprite %s"></div></div>%s</div>';
	// var tpl  = '<div data-id="%s" data-name="%s"  class="icon-wrap"><div  class="liao_css3 hi-icon icon-btn hi-icon-mobile"><div  class="  sprite %s"></div></div>%s</div>';
	var html = '';
	_.each(btns, function(v, k) {
		var name = v.name || '';
		var id   = v.id || '';
		var txt  = v.txt ? util.format('<div class="txt">%s</div>', __(v.txt)) : '<div class="txt"> </div>';
		html += util.format(tpl, id,name, name, name, txt);
	});
	$el.html(html);
};

window.icons = icons;

})(window);